﻿package morn.core.components
{
    import __AS3__.vec.*;
    import flash.display.*;
    import morn.core.handlers.*;

    public class ViewStack extends Box implements IItem
    {
        protected var _items:Vector.<DisplayObject>;
        protected var _setIndexHandler:Handler;
        protected var _selectedIndex:int;

        public function ViewStack()
        {
            this._setIndexHandler = new Handler(this.setIndex);
            return;
        }// end function

        public function setItems(param1:Array) : void
        {
            var _loc_5:DisplayObject = null;
            removeAllChild();
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:* = param1.length;
            while (_loc_3 < _loc_4)
            {
                
                _loc_5 = param1[_loc_3];
                if (_loc_5)
                {
                    _loc_5.name = "item" + _loc_2;
                    addChild(_loc_5);
                    _loc_2++;
                }
                _loc_3++;
            }
            this.initItems();
            return;
        }// end function

        public function addItem(param1:DisplayObject) : void
        {
            param1.name = "item" + this._items.length;
            addChild(param1);
            this.initItems();
            return;
        }// end function

        public function initItems() : void
        {
            var _loc_2:DisplayObject = null;
            this._items = new Vector.<DisplayObject>;
            var _loc_1:int = 0;
            while (_loc_1 < int.MAX_VALUE)
            {
                
                _loc_2 = getChildByName("item" + _loc_1);
                if (_loc_2 == null)
                {
                    break;
                }
                this._items.push(_loc_2);
                _loc_2.visible = _loc_1 == this._selectedIndex;
                _loc_1++;
            }
            return;
        }// end function

        public function get selectedIndex() : int
        {
            return this._selectedIndex;
        }// end function

        public function set selectedIndex(param1:int) : void
        {
            if (this._selectedIndex != param1)
            {
                this.setSelect(this._selectedIndex, false);
                this._selectedIndex = param1;
                this.setSelect(this._selectedIndex, true);
            }
            return;
        }// end function

        protected function setSelect(param1:int, param2:Boolean) : void
        {
            if (this._items && param1 > -1 && param1 < this._items.length)
            {
                this._items[param1].visible = param2;
            }
            return;
        }// end function

        public function get selection() : DisplayObject
        {
            return this._selectedIndex > -1 && this._selectedIndex < this._items.length ? (this._items[this._selectedIndex]) : (null);
        }// end function

        public function set selection(param1:DisplayObject) : void
        {
            this.selectedIndex = this._items.indexOf(param1);
            return;
        }// end function

        public function get setIndexHandler() : Handler
        {
            return this._setIndexHandler;
        }// end function

        public function set setIndexHandler(param1:Handler) : void
        {
            this._setIndexHandler = param1;
            return;
        }// end function

        protected function setIndex(param1:int) : void
        {
            this.selectedIndex = param1;
            return;
        }// end function

        public function get items() : Vector.<DisplayObject>
        {
            return this._items;
        }// end function

        override public function set dataSource(param1:Object) : void
        {
            var _loc_2:String = null;
            _dataSource = param1;
            if (param1 is int || param1 is String)
            {
                this.selectedIndex = int(param1);
            }
            else
            {
                for (_loc_2 in _dataSource)
                {
                    
                    if (hasOwnProperty(_loc_2))
                    {
                        this[_loc_2] = _dataSource[_loc_2];
                    }
                }
            }
            return;
        }// end function

    }
}
